package com.android.shop.api;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.shop.bean.Dish;
import com.android.shop.bean.DishCart;
import com.android.shop.bean.OrderDetailsBean;
import com.android.shop.bean.Order;
import com.android.shop.bean.SellerBean;
import android.util.Log;

public class MyHttpClient {
	HttpPost post;
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	private String server2client = "";
	public String PATH = "http://115.29.165.190/api.aspx?";
	private static MyHttpClient httpCt = new MyHttpClient();

	public static MyHttpClient getSingle() {
		return httpCt;
	}

	private String ServerToClient(String url, final List<NameValuePair> params) {
		post = new HttpPost(url);
		HttpParams httpParameters = new BasicHttpParams();
		try {
			if (params != null) {
				UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
						params, "utf-8");
				post.setEntity(urlEncodedFormEntity);
			}
			HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
			HttpClient httpClient = new DefaultHttpClient(httpParameters);
			HttpResponse re = httpClient.execute(post);
			if (re.getStatusLine().getStatusCode() == 200) {
				server2client = EntityUtils.toString(re.getEntity());
				return server2client.toString();
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.i("请求接口错误", e.toString());
			return null;
		} finally {
			server2client = null;
		}
	}

	/**
	 * 根据商家获取类别
	 */
	public List<DishCart> getCatsBySeller(String sellerid) {
		String method = "dish.catsbyseller.get";
		List<DishCart> lst = new ArrayList<DishCart>();
		final String url = PATH + "method=" + method + "&sellerid=" + sellerid;
		Log.i("url", url);
		String result = ServerToClient(url, null);
		if (result != null) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("SellerCategoryList");
				for (int i = 0; i < jay.length(); i += 1) {
					JSONObject temp = (JSONObject) jay.get(i);
					DishCart cart = new DishCart();
					cart.setId(temp.getString("id"));
					cart.setCartname(temp.getString("catname"));
					lst.add(cart);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return lst;
		} else {
			return null;
		}
	}

	/*
	 * 添加菜品
	 */
	public String addDish(String dishname, String price, String catid,
			String photo, String state, String sellerid, String dishid,
			String type) {
		String method = "dish.addupdate";
		final String url = PATH + "method=" + method + "&dishname=" + dishname
				+ "&price=" + price + "&catid=" + catid + "&photo=" + photo
				+ "&sellerid=" + sellerid + "&state=" + state + "&dishid="
				+ dishid + "&type=" + type;
		System.out.println(url);
		String result = ServerToClient(url, null);
		if (result != null) {
			return result;
		} else {
			return "";
		}
	}

	/**
	 * 分页获取的订单
	 */
	public List<Order> getOrderForm(String sellerid, String ordertype,
			String state, String operate, String id) {
		String method = "order.sellerorder.get";
		List<Order> lst = new ArrayList<Order>();
		final String url = PATH + "method=" + method + "&sellerid=" + sellerid
				+ "&ordertype=" + ordertype + "&state=" + state + "&operate="
				+ operate + "&id=" + id;
		Log.i("url", url);
		String result = ServerToClient(url, null);
		if (result != null) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("SellerOrderList");
				for (int i = 0; i < jay.length(); i += 1) {
					JSONObject temp = (JSONObject) jay.get(i);
					Order bean = new Order();
					bean.setId_of(temp.getString("id"));
					bean.setOrdersn_of(temp.getString("ordersn"));
					bean.setAllprice_of(temp.getString("allprice"));
					bean.setBuyerid_of(temp.getString("buyerid"));
					bean.setPhone_of(temp.getString("phone"));
					bean.setAddTime_of(temp.getString("addtime"));
					bean.setScheduledTime_of(temp.getString("scheduledtime"));
					bean.setPopulation_of(temp.getString("population"));
					bean.setRemark_of(temp.getString("remark"));
					bean.setState_of(temp.getString("state"));
					bean.setOrderType_of(temp.getString("ordertype"));
					bean.setSendAdress_of(temp.getString("sendaddress"));
					bean.setSendPrice_of(temp.getString("sendprice"));
					bean.setDishPrice_of(temp.getString("dishprice"));
					bean.setSellerid_of(temp.getString("sellerid"));
					lst.add(bean);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return lst;
		} else {
			return null;
		}
	}
	
	public List<Order> getOrderList(String userid,String usertype, String ordertype,
			String state, String operate, String id) {
		String method = "order.sellerorder.get";
		List<Order> lst = new ArrayList<Order>();
		final String url = PATH + "method=" + method + "&userid=" + userid+"&usertype="+usertype
				+ "&ordertype=" + ordertype + "&state=" + state + "&operate="
				+ operate + "&id=" + id;
		Log.i("url", url);
		String result = ServerToClient(url, null);
		if (result != null) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("OrderList");
				for (int i = 0; i < jay.length(); i += 1) {
					JSONObject temp = (JSONObject) jay.get(i);
					Order bean = new Order();
					bean.setId_of(temp.getString("id"));
					bean.setOrdersn_of(temp.getString("ordersn"));
					bean.setAllprice_of(temp.getString("allprice"));
					bean.setBuyerid_of(temp.getString("buyerid"));
					bean.setPhone_of(temp.getString("phone"));
					bean.setAddTime_of(temp.getString("addtime"));
					bean.setScheduledTime_of(temp.getString("scheduledtime"));
					bean.setPopulation_of(temp.getString("population"));
					bean.setRemark_of(temp.getString("remark"));
					bean.setState_of(temp.getString("state"));
					bean.setOrderType_of(temp.getString("ordertype"));
					bean.setSendAdress_of(temp.getString("sendaddress"));
					bean.setSendPrice_of(temp.getString("sendprice"));
					bean.setDishPrice_of(temp.getString("dishprice"));
					bean.setSellerid_of(temp.getString("sellerid"));
					lst.add(bean);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return lst;
		} else {
			return null;
		}
	}
	
	/**
	 * 获取订单详情
	 */
	public List<OrderDetailsBean> getOrderDetails(String orderid) {
		String method = "order.sellerorderinfo.get";
		List<OrderDetailsBean> lst = new ArrayList<OrderDetailsBean>();
		final String url = PATH + "method=" + method + "&orderid=" + orderid;
		Log.i("url", url);
		String result = ServerToClient(url, null);
		if (result != null) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("SellerOrderDetail");
				for (int i = 0; i < jay.length(); i += 1) {
					JSONObject temp = (JSONObject) jay.get(i);
					OrderDetailsBean bean = new OrderDetailsBean();
					bean.setDishid(temp.getString("dishid"));
					bean.setId(temp.getString("id"));
					bean.setCount(temp.getString("count"));
					bean.setDishname(temp.getString("dishname"));
					bean.setPrice(temp.getString("price"));
					bean.setAllprice(temp.getString("allprice"));
					lst.add(bean);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return lst;
		} else {
			return null;
		}
	}
	/**
	 * 处理订单
	 */
	public String updateOrder(String orderid, String modify, String usertype) {
		String method = "order.update";
		final String url = PATH + "method=" + method + "&orderid=" + orderid
				+ "&modify=" + modify + "&usertype=" + usertype;
		System.out.println(url);
		String result = ServerToClient(url, null);
		if (result != null) {
			return result;
		} else {
			return "";
		}
	}

	/*
	 * 通过商户id和状态查询菜品
	 */
	public List<Dish> getDishBySellerid(String sellerid, String state) {
		String method = "dish.alldishbyseller.get";
		List<Dish> listDish = new ArrayList<Dish>();
		String url = PATH + "method=" + method + "&sellerid=" + sellerid
				+ "&state=" + state;
		Log.i("getDishBySellerid", url);
		String result = ServerToClient(url, null);
		if (result != null&&!result.equals("NULL")) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("AllSellerDish");
				for (int i = 0; i < jay.length(); i++) {
					JSONObject temp = (JSONObject) jay.get(i);
					Dish bean = new Dish();
					bean.setDishid(temp.getString("id"));
					bean.setDishname(temp.getString("dishname"));
					bean.setPrice(temp.getString("price"));
					bean.setState(temp.getString("state"));
					bean.setPhoto(temp.getString("photo"));
					listDish.add(bean);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return listDish;
	}

	/*
	 * 通过菜品id修改状̬
	 */
	public String modifyDishState(String dishid) {
		String method = "dish.DishState.modify";
		String url = PATH + "method=" + method + "&dishid=" + dishid;
		Log.i("dish.DishState.modify", url);
		String result = ServerToClient(url, null);
		return result;
	}

	/*
	 * 通过sellerid获取商户信息
	 */
	public SellerBean getSeller(String sellerid) {
		String method = "user.sellerbyid.get";
		String url = PATH + "method=" + method + "&sellerid=" + sellerid;
		Log.i("getSeller", url);
		String result = ServerToClient(url, null);
		SellerBean seller = new SellerBean();
		if (result != null) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("Seller");
				JSONObject temp = (JSONObject) jay.get(0);
				seller.setSellerid(temp.getString("id"));
				seller.setArrivtime(temp.getString("arrivtime"));
				seller.setMinprice(temp.getString("floorprice"));
				seller.setPhone(temp.getString("phone"));
				seller.setSendneedprice(temp.getString("tip"));
				seller.setState(temp.getString("state"));
				seller.setAverage_spend(temp.getString("average_spend"));
				seller.setShopname(temp.getString("shopname"));
				seller.setShopphoto(temp.getString("shopphoto"));
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		return seller;
	}

	/*
	 * 修改商户信息
	 */
	public String modifySeller(String sellerid, String arrivtime,
			String minprice, String phone, String sendneedprice,
			String average_spend) {
		String method = "user.seller.modify";
		String url = PATH + "method=" + method + "&sellerid=" + sellerid
				+ "&arrivtime=" + arrivtime + "&minprice=" + minprice
				+ "&phone=" + phone + "&sendneedprice=" + sendneedprice
				+ "&average_spend=" + average_spend;
		Log.i("user.seller.modify", url);
		String result = ServerToClient(url, null);
		return result;
	}

	/*
	 * 修改商户状态̬
	 */
	public String modifySellerState(String sellerid) {
		String method = "user.sellerstate.modify";
		String url = PATH + "method=" + method + "&sellerid=" + sellerid;
		Log.i("user.sellerstate.modify", url);
		String result = ServerToClient(url, null);
		return result;
	}

	/*
	 * 登陆
	 */
	public String Login(String loginname, String pwd) {
		String method = "user.seller.login";
		if (loginname.equals("") || pwd.equals("")) {
			return "NOT EMPTY";
		}
		String url = PATH + "method=" + method + "&loginname=" + loginname
				+ "&pwd=" + pwd;
		Log.i("user.login", url);
		String result = ServerToClient(url, null);
		return result;
	}

	/*
	 * 通过dishid获取菜品
	 */
	public Dish getDishById(String dishid) {
		String method = "dish.dishdetail.get";
		String url = PATH + "method=" + method + "&dishid=" + dishid;
		Log.i("dish.dishdetail.get", url);
		String result = ServerToClient(url, null);
		Dish bean = new Dish();
		if (result != null) {
			JSONObject job;
			try {
				job = new JSONObject(result);
				JSONArray jay = job.getJSONArray("DishDetail");
				JSONObject temp = (JSONObject) jay.get(0);
				bean.setDishid(temp.getString("id"));
				bean.setDishname(temp.getString("dishname"));
				bean.setPrice(temp.getString("price"));
				bean.setState(temp.getString("state"));
				bean.setPhoto(temp.getString("photo"));
				bean.setCartid(temp.getString("catid"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bean;
	}
	
	/*
	 * 上传图片
	 */
	public void uploadPhoto(String fileName, String imageBuffer,String filetype){
		String method = "photo.upload";
		String url = PATH + "method=" + method + "&filename=" + fileName+"&filetype="+filetype+"&imageBuffer="+imageBuffer;
		String result = ServerToClient(url, null);
	}

}
